package com.ghca.myinfo.system.entities;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.Set;

/**
 * Created by Administrator on 2017/1/24.
 */
@Entity
public class User implements UserDetails{

    public static final Integer AVAILABLE = 0;
    public static final Integer ACCOUNT_EXPIRED = 1;
    public static final Integer ACCOUNT_LOCKED = 2;
    public static final Integer CREDENTIALS_EXPIRED = 4;



    @Id
    @GeneratedValue(generator = "id")
    @GenericGenerator(strategy = "uuid2", name = "id")
    private String id;

    private String password;
    private String username;
    private String realname;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable
    private Set<Role> roles;

    private int status;



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }



    @Override
    public boolean isAccountNonExpired() {
        return !((ACCOUNT_EXPIRED & getStatus()) == ACCOUNT_EXPIRED);
    }


    @Override
    public boolean isAccountNonLocked() {
        return !((ACCOUNT_LOCKED & getStatus()) == ACCOUNT_LOCKED);
    }


    @Override
    public boolean isCredentialsNonExpired() {
        return !((CREDENTIALS_EXPIRED & getStatus()) == CREDENTIALS_EXPIRED);
    }


    @Override
    public boolean isEnabled() {
        return getStatus() == AVAILABLE;
    }


    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setAuthorities(Set<Role> roles) {
        this.roles = roles;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    @Override
    public Set<Role> getAuthorities() {
        return roles;
    }
}
